package com.hlk.article.html.consume;

import com.hlk.article.html.controller.ArticleHTMLComponent;
import com.hlk.config.RabbitmqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author hlk
 * @version 1.0
 * @date 2021-01-15 10:24
 */
@Component
public class ArticleConsume {
    @Autowired
    private ArticleHTMLComponent articleHTMLComponent;

    @RabbitListener(queues = {RabbitmqConfig.QUEUE_DOWNLOAD_HTML})
    public void watchQueue(String payload, Message message) {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();

        if (routingKey.equalsIgnoreCase("article.html.download")) {
            // 下载html
            String[] split = payload.split(",");
            try {
                articleHTMLComponent.download(split[0], split[1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (routingKey.equalsIgnoreCase("article.html.delete")) {

            articleHTMLComponent.delete(payload);

        }
    }

}
